package com.fulu.game.bi.service.impl.product;

import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.entity.bo.channel.EventAnalyseDetailListBO;
import com.fulu.game.bi.entity.dto.EventAnalyseDetailListDTO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.product.EventAnalyseDetailListVO;
import com.fulu.game.bi.hbase.BehaviourEventDetailStatDao;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BehaviourEventDetailStatService {
    @Autowired
    private BehaviourEventDetailStatDao behaviourEventDetailStatDao;


    public static final Map<Integer, String> behaviourEventDetailStatMap;
    public static final Map<Integer, String> behaviourEventStatMap;

    static {
        behaviourEventStatMap = new HashMap<>();
        behaviourEventStatMap.put(QueryTypeStatusEnum.DAY.getType(), "behaviour_event_stat_day");
        behaviourEventStatMap.put(QueryTypeStatusEnum.WEEK.getType(), "behaviour_event_stat_week");
        behaviourEventStatMap.put(QueryTypeStatusEnum.MONTH.getType(), "behaviour_event_stat_month");
    }

    static {
        behaviourEventDetailStatMap = new HashMap<>();
        behaviourEventDetailStatMap.put(QueryTypeStatusEnum.DAY.getType(), "behaviour_event_detail_stat_day");
        behaviourEventDetailStatMap.put(QueryTypeStatusEnum.WEEK.getType(), "behaviour_event_detail_stat_week");
        behaviourEventDetailStatMap.put(QueryTypeStatusEnum.MONTH.getType(), "behaviour_event_detail_stat_month");
    }

    public PageInfo<EventAnalyseDetailListVO> listDetail(EventAnalyseDetailListDTO dto) {
        EventAnalyseDetailListBO bo = new EventAnalyseDetailListBO();
        BeanUtils.copyProperties(dto, bo);
        bo.setPageNum((dto.getPageNum() - 1) * dto.getPageSize());
        bo.setTableName(behaviourEventDetailStatMap.get(dto.getQueryType()));
        bo.setJoinTable(behaviourEventStatMap.get(dto.getQueryType()));
        List<EventAnalyseDetailListVO> list = behaviourEventDetailStatDao.listDetail(bo);
        Integer total = behaviourEventDetailStatDao.total(bo);
        list.stream().forEach(map -> {
            map.setQueryType(dto.getQueryType());
        });
        PageInfo<EventAnalyseDetailListVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(total == null ? 0 : total);
        return pageInfo;
    }

    public void listDetailExport(EventAnalyseDetailListDTO dto) {
        String fileName = "事件分析详情列表";
        LinkedHashMap<String, Mapper<EventAnalyseDetailListVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(dto.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("参数值", v -> v.getAttributeValue());
        map.put("触发用户数", v -> v.getTriggerUserNum() + "");
        map.put("触发次数", v -> v.getTriggerNum() + "");
        map.put("人数占比", v -> v.getUserRatio().toString());
        Querier querier = (num) -> listDetail(dto);
        CsvExportUtil.export(100, fileName, map, querier);
    }
}
